package dao;

import entity.Movie;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MovieDaoImpl implements IMovieDao {
    private static Map<String, Movie> movieMap = new ConcurrentHashMap<>();

    @Override
    public boolean addMovie(Movie movie) {
        if (movieMap.containsKey(movie.getMovieId())) {
            return false;
        }
        movieMap.put(movie.getMovieId(), movie);
        return true;
    }

    @Override
    public boolean deleteMovie(String movieId) {
        if (!movieMap.containsKey(movieId)) {
            return false;
        }
        movieMap.remove(movieId);
        return true;
    }

    @Override
    public boolean updateMovie(Movie movie) {
        if (!movieMap.containsKey(movie.getMovieId())) {
            return false;
        }
        movieMap.put(movie.getMovieId(), movie);
        return true;
    }

    @Override
    public Movie getMovieById(String movieId) {
        return movieMap.get(movieId);
    }

    @Override
    public List<Movie> getAllMovies() {
        return new ArrayList<>(movieMap.values());
    }

    @Override
    public List<Movie> searchMovies(String name, String director, String stars) {
        List<Movie> result = new ArrayList<>();
        for (Movie movie : movieMap.values()) {
            boolean matchName = name == null || name.isEmpty() || movie.getName().contains(name);
            boolean matchDirector = director == null || director.isEmpty() || movie.getDirector().contains(director);
            boolean matchStars = stars == null || stars.isEmpty() || movie.getStars().contains(stars);
            if (matchName && matchDirector && matchStars) {
                result.add(movie);
            }
        }
        return result;
    }
}